package homework.Day0324.拉火车;

import java.util.*;
import java.util.concurrent.TimeUnit;

public class PokerGame {
    private static final Queue<String> player1 = new LinkedList<>();
    private static final Queue<String> player2 = new LinkedList<>();
    private static final List<String> deck = new ArrayList<>();
    private static boolean b = false;

    public static void main(String[] args) {
        System.out.println("开始发牌。。。");
        String[] patterns = {"♥", "♦", "♠", "♣"};
        String[] faces = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
        String[] cards = new String[54];
        int cardsIndex = 0;
        for (String pattern : patterns) {
            for (String face : faces) {
                cards[cardsIndex++] = pattern + face;
            }
        }
        cards[52] = "小王";
        cards[53] = "大王";

        Random rand = new Random();
        String temp;
        int index;
        for (int i = 0; i < cards.length; i++) {
            index = rand.nextInt(i + 1);
            temp = cards[i];
            cards[i] = cards[index];
            cards[index] = temp;
        }

        player1.addAll(Arrays.asList(cards).subList(0, 27));

        player2.addAll(Arrays.asList(cards).subList(27, 54));

        while (true){

            if (player1.size() == 0){
                System.out.println("玩家2获胜");
                return; }
            if (player2.size() == 0){
                System.out.println("玩家1获胜");
                return; }

            System.out.println("玩家一：");
            for (String card : player1
                 ) { System.out.print(card + " "); }

            System.out.println("玩家二：");
            for (String card : player2
            ) { System.out.print(card + " "); }

            System.out.println("牌桌：");
            for (String card : deck
            ) { System.out.print(card + " "); }

            String one = player1.remove();
            deck.add(one);
            Iterator<String> iterator = deck.iterator();
            boolean nice = false;
            while (iterator.hasNext()){
                if (nice){
                    player1.add(iterator.next());
                    iterator.remove();
                    continue;
                }
                String tableCard = iterator.next();
                if (tableCard.substring(1).equals(one.substring(1))){
                    nice = true;
                    player1.add(tableCard);
                    iterator.remove();
                }
            }

            String two = player2.remove();
            deck.add(two);
            iterator = deck.iterator();


        }
    }

}
